---
title: Transformer.js
sidebar:
    order: 20
description: Implement summarization with Transformers.js and leverage local
    hardware acceleration
keywords: Transformers.js, summarization, onnxruntime, CPU/GPU acceleration,
    pretrained models
---

:::caution

We have temporarily removed support for the `transformers` to reduce the installation footprint.

:::

HuggingFace [Transformers.js](https://huggingface.co/docs/transformers.js/index) is a JavaScript library
that lets you run pretrained models locally on your machine. The library uses [onnxruntime](https://onnxruntime.ai/)
to leverage the CPU/GPU capabilities of your hardware.

In this guide, we will show how to create [summaries](https://huggingface.co/tasks/summarization) using the [Transformers.js](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.SummarizationPipeline) library.

:::tip

Transformers.js has an extensive list of tasks available. This guide will only cover one but checkout their [documentation](https://huggingface.co/docs/transformers.js/pipelines#tasks)
for more.

:::

## Import the pipeline

The snippet below imports the Transformers.js library and loads the summarizer pipeline and model.
You can specify a model name or let the library pick the latest and greatest.

```js
import { pipeline } from "genaiscript/runtime"
const summarizer = await pipeline("summarization")
```

Allocating and loading the model can take some time,
so it's best to do this at the beginning of your script
and only once.

:::note[Migrate your script to `.mjs`]

To use the `Transformers.js` library, you need to use the `.mjs` extension for your script (or `.mts` for TypeScript support).
If your script is ending in `.genai.js`, rename it to `.genai.mjs`.

:::

## Invoke the pipeline

The summarizer pipeline has a single argument, the content to summarize. It returns an array of summaries
which we need to unpack to access the final summary text. This is what we do below and `summary_index` contains the summary text.

```js
const [summary] = await summarizer(content)
// @ts-ignore
const { summary_text } = summary
```
